package com.autoswf.automator
{
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	
	/**
	 * Sends event descriptors over local connection, accepts and loads event descriptors into AutomationObserver.
	 */
	public class LCAutomationClient implements IAutomationClient
	{
		protected var lc:LocalConnection;
		protected var _automationObserver:IAutomationObserver;
		protected var _automationClient:IAutomationClient;
		
		protected var _txId:String = "_autoswf.automationTx";
		protected var _rxId:String = "_autoswf.automationRx";
		
		
		/**
		 * Constructor.
		 */
		public function LCAutomationClient(automationClient:IAutomationClient = null)
		{
			_automationClient = automationClient;
			
			lc = new LocalConnection();
			lc.addEventListener(StatusEvent.STATUS, lc_statusEventHandler);
			lc.client = this;
			lc.connect(_rxId);
		}


		/** Receive an event descriptor xml over local connection and pass to the AutomationObserver to simulate. */
		public function lc_remoteLoadCommand(descriptor:String):void
		{
			_automationObserver.notify(descriptor);  /* send to observer to interpret */
		}


		/** Broadcast a descriptor over local connection. */
		public function handle(descriptor:String):void
		{
			lc.send(_txId, "lc_notify", descriptor);
			
			if(_automationClient)
			{
				_automationClient.handle(descriptor);
			}
		}
		
		
		/** Receive a descriptor over local connection. */
		public function set observer(value:IAutomationObserver):void
		{
			_automationObserver = value;
		}
		
		
		protected function lc_statusEventHandler(event:StatusEvent):void
		{
//			trace("Sending on LC: \""+_connectionId+"\" failed, receiving connection probably wasn't opened yet.");
		}
		
	}
}